<?php

class mproducts extends CI_Model {

    private $tbl = "products";
    
    public function __construct() {
        parent::__construct();
        $cond = array('qty >' => 0);
        $stt = array('status' => 1);
        $this->db->where($cond)->update($this->tbl, $stt);
        $cond1 = array('qty <=' => 0);
        $stt1 = array('status' => 0);
        $this->db->where($cond1)->update($this->tbl, $stt1);
    }

    public function fetchAll($numb = NULL, $off = NULL, $ob = null) {
        if ($numb == Null && $off == NULL)
            return $this->db->order_by("id", "desc")->get($this->tbl)->result();
        else {
            if ($ob == Null)
                return $this->db->order_by("id", "desc")->get($this->tbl, $numb, $off)->result();
            else
                return $this->db->order_by("price", $ob)->get($this->tbl, $numb, $off)->result();
        }
    }

    public function fetchOne($id) {
        return $this->db->where('id', $id)->get($this->tbl)->row();
    }

    public function insert($arr = array()) {
        $this->db->insert($this->tbl, $arr);
    }

    public function update($id, $arr = array()) {
        $this->db->where('id', $id)->update($this->tbl, $arr);
    }
    
    public function updateQty1($id, $qty) {
        $data = $this->fetchOne($id);
        $count = $data->qty - $qty;
        $arr = array('qty' => $count);
        $this->db->where('id', $id)->update($this->tbl, $arr);      
    }
    
    public function updateQty2($id, $qty) {
        $data = $this->fetchOne($id);
        $count = $data->qty + $qty;
        $arr = array('qty' => $count);
        $this->db->where('id', $id)->update($this->tbl, $arr);      
    }

    public function delete($id) {
        $this->db->delete($this->tbl, array('id' => $id));
    }

    public function deleteAll() {
        $this->db->empty_table($this->tbl);
    }
    
    public function deleteByCid($id) {
        $this->db->where('cid', $id)->or_where('pid', $id)->delete($this->tbl);
    }

    public function getProByPid($id = NULL, $numb = NULL, $off = NULL, $ob = null) {
        if ($numb == NULL && $off == NULL)
            return $this->db->where('pid', $id)->order_by("id", "desc")->get($this->tbl)->result();
        else {
            if ($ob == Null)
                return $this->db->where('pid', $id)->order_by("id", "desc")->get($this->tbl, $numb, $off)->result();
            else
                return $this->db->where('pid', $id)->order_by("price", $ob)->get($this->tbl, $numb, $off)->result();
        }
    }

    public function getProByCid($id = NULL, $numb = NULL, $off = NULL, $ob = null) {
        if ($numb == Null && $off == NULL)
            return $this->db->where('cid', $id)->order_by("id", "desc")->get($this->tbl)->result();
        else {
            if ($ob == Null)
                return $this->db->where('cid', $id)->order_by("id", "desc")->get($this->tbl, $numb, $off)->result();
            else
                return $this->db->where('cid', $id)->order_by("price", $ob)->get($this->tbl, $numb, $off)->result();
        }
    }

    public function getProBySear($text = "", $numb = NULL, $off = NULL) {
        if ($numb == Null && $off == NULL)
            return $this->db->like('name', $text)->or_like('des', $text)->order_by("id", "desc")->get($this->tbl)->result();
        else
            return $this->db->like('name', $text)->or_like('des', $text)->order_by("id", "desc")->get($this->tbl, $numb, $off)->result();
    }

    public function getProRelate($id, $cid, $price) {
        $array = array('id !=' => $id, 'cid' => $cid, 'price <' => $price + 3000000, 'price >' => $price - 3000000);
        return $this->db->where($array)->order_by("id", "desc")->get($this->tbl)->result();
    }
    

}
